package com.zhang.service;



import com.zhang.Email2AsyncSendApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


@Service
public class AsyncService {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties;


    //发送内容为html
    @Async
    public void email() throws Exception {

        Thread.sleep(3000);//模拟邮箱缓慢发送

        //1.封装消息体
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setSubject("邮件标题");
        helper.setText("<h1>验证码: adf45</h1>",true);
        helper.setFrom(mailProperties.getUsername());//邮件发送者
        helper.setTo("13512336082@163.com");//邮件接收者

        //2.发送
        javaMailSender.send(mimeMessage);
    }

}

























